<template>
  <div class="box">
    <div class="nav">
      <ul class="location">
        <li>
          <Dropdown placement="bottom-start">
            <a href="javascript:void(0)" style="color:grey">
              <Icon type="ios-location" class="icon"></Icon>
              送至:{{city}}
            </a>
            <DropdownMenu slot="list">
              <div class="city">
                <p v-for="(items, index) in cityArr" :key="index">
                  <span
                    v-for="(item, index) in items"
                    class="city-item"
                    :key="index"
                    @click="changeCity(item)"
                  >{{item}}</span>
                </p>
              </div>
            </DropdownMenu>
          </Dropdown>
        </li>
      </ul>
      <ul class="detail">
        <li class="first" v-show="!this.token">
          你好，请
          <router-link to="/">
            登录
            <Icon type="md-person"></Icon>
          </router-link>|
          <span class="text-color-red">
            <router-link to="register">
              免费注册
              <Icon type="md-person-add"></Icon>
            </router-link>
          </span>
        </li>
        <li v-show="this.token">
          <Dropdown>
            <p class="username-p">
              <Avatar src="https://i.loli.net/2017/08/21/599a521472424.jpg" />
              <span class="username">{{this.username}}</span>
            </p>
            <DropdownMenu slot="list">
              <div class="my-page">
                <div class="my-info" @click="myInfo">
                  <p>
                    <Icon type="md-home"></Icon>
                  </p>
                  <p>个人主页</p>
                </div>
                <div class="sign-out" @click="signOutFun">
                  <p>
                    <Icon type="md-undo"></Icon>
                  </p>
                  <p>退出登录</p>
                </div>
              </div>
            </DropdownMenu>
          </Dropdown>
        </li>
        <!-- <el-badge :value="12" class="item">
          <a @click="goShopCart">
              <Icon type="ios-cart-outline">
                <Badge :count="3">
                  <a href="#" class="demo-badge"></a>
                </Badge>
              </Icon>购物车
            </a>
        </el-badge>-->
        <li>
          <Dropdown placement="bottom-start">
            <a @click="goShopCart">
              <Icon type="ios-cart-outline">
                <Badge :count="3">
                  <a href="#" class="demo-badge"></a>
                </Badge>
              </Icon>购物车
            </a>
          </Dropdown>
        </li>
        <li>
          <router-link to="/freeback">书店公告</router-link>
        </li>
        <li>
          <router-link to="/allgoods">全部商品</router-link>
        </li>
        <li>
          <router-link to="/home">书店首页</router-link>
        </li>
        <!-- <li>
          <Button @click="value1 = true" type="primary">Open</Button>
          <Drawer title="Basic Drawer" :closable="true" v-model="value1">
            <p>Some contents...</p>
            <p>Some contents...</p>
            <p>Some contents...</p>
          </Drawer>
        </li>-->
        {{userid}}
      </ul>
    </div>
  </div>
</template>

<script>
import New from "@/new.js";
export default {
  name: "M-Header",
  created() {
    // 通过同级组件传值，保存用户登录的状态
    New.$on("token", data => {
      console.log(this.$store.state.userinfo);
      this.token = data;
    });
  },
  data() {
    return {
      value1: false,
      userinfo: this.$store.state.userinfo,
      userid: window.sessionStorage.userid,
      username: window.sessionStorage.username,
      token: window.sessionStorage.token,
      control: "",
      city: "广州",
      cityArr: [
        ["北京", "上海", "天津", "重庆", "广州"],
        ["深圳", "河南", "辽宁", "吉林", "江苏"],
        ["江西", "四川", "海南", "贵州", "云南"],
        ["西藏", "陕西", "甘肃", "青海", "珠海"]
      ]
    };
  },
  computed: {},
  methods: {
    changeCity(city) {
      this.city = city;
    },
    goToPay() {
      this.$router.push("/order");
    },
    myInfo() {
      this.$router.push("/personal");
    },
    signOutFun() {
      sessionStorage.clear();
      // sessionStorage.removeItem("token");
      // sessionStorage.removeItem("username");
      // sessionStorage.removeItem("userid");
      this.token = "";
      this.$router.push("/");
    },
    goShopCart() {
      if (window.sessionStorage.token) {
        this.$router.push("/personal/myShoppingCart");
      } else {
        // this.$message.error("请先登录！");
        var message=confirm('是否前往登陆')
        if(message==true){
          this.$router.push("/");
        }
      }
    }
  }
};
</script>

<style scoped>
.box {
  width: 100%;
  height: 65px;
  /* background-color: #e3e4e5; */
  background-color: thistle;
}
.nav {
  margin: 0 auto;
  width: 90%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.nav ul {
  list-style: none;
}
.nav li {
  float: left;
  font-size: 14px;
  line-height: 65px;
  margin-right: 15px;
  font-weight: bold;
}
.nav a {
  font-size: 18px;
  text-decoration: none;
  color: white;
  padding-left: 15px;
  border-left: 1px solid #ccc;
  cursor: pointer;
}
.location a {
  border-left: none;
}
.nav a:hover {
  color: #d9534f;
}
.location {
  color: #999999;
}
.icon {
  color: #d9534f;
}
.first {
  color: #999999;
}
.first a:first-child {
  padding-left: 3px;
  border-left: none;
}
.city {
  padding: 10px 15px;
}
.city-item {
  font-weight: bold;
  cursor: pointer;
  padding: 5px;
}
.city-item:hover {
  color: #d9534f;
}
.person-icon {
  color: #d9534f;
  background-color: #f0cdb2;
}
.username {
  color: #999999;
}
.shopping-cart-list {
  padding: 3px 15px;
}
.shopping-cart-box {
  margin: 8px 0px;
  margin-top: 15px;
  padding-bottom: 15px;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px #ccc dotted;
}
.shopping-cart-box:first-child {
  margin-top: 8px;
}
.shopping-cart-img {
  margin-right: 15px;
  width: 40px;
  height: 40px;
}
.shopping-cart-img img {
  width: 100%;
}
.shopping-cart-info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: space-between;
  width: 200px;
  overflow: hidden;
  font-size: 12px;
  line-height: 20px;
  color: #999999;
}
.shopping-cart-detail {
  color: #999999;
}
.shopping-cart-text {
  color: #ccc;
}
.go-to-buy {
  display: flex;
  justify-content: flex-end;
}
.shopping-cart-null {
  padding: 15px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.cart-null-icon {
  font-size: 38px;
  margin-bottom: 15px;
}
.shopping-cart-null span {
  color: #999999;
  font-size: 12px;
  line-height: 16px;
}
.username-p {
  cursor: pointer;
}
.my-page {
  padding: 3px 5px;
  width: 180px;
  height: 100%;
  display: flex;
  justify-content: center;
  /* align-items: center; */
}
.my-page a {
  margin: 0px;
  padding: 0px;
  border: none;
}
.my-info {
  padding: 0px;
  width: 50%;
  height: 100%;
  text-align: center;
  cursor: pointer;
}
.my-info p:nth-child(2) {
  margin-top: -30px;
}
.my-info:hover {
  box-shadow: 0px 0px 5px #ccc;
}
.my-info i {
  font-size: 28px;
}
.my-info p {
  font-size: 12px;
}
.sign-out {
  width: 50%;
  height: 100%;
  text-align: center;
  cursor: pointer;
}
.sign-out p:nth-child(2) {
  margin-top: -30px;
}
.sign-out:hover {
  box-shadow: 0px 0px 5px #ccc;
}
.sign-out i {
  font-size: 28px;
}
.item {
  /* margin-top: 5px;
  margin-right: 40px; */
}
</style>

